<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-29 19:51:21 +0200 (Sun, 29 Jun 2008) $
 * $Rev: 1238 $
 * $Author: gavinspearhead $
 * $Id: admin_tasks.php 1238 2008-06-29 17:51:21Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);


$pathadt = realpath(dirname(__FILE__));

require_once "$pathadt/../functions/ajax_includes.php";
require_once "$pathadt/../functions/pref_functions.php";


$urdd_online = check_urdd_online();

if (!$isadmin)
	fatal_error($LN['error_noadmin']);

$sort = get_request('sort', 'lastupdate');
$sort_dir = get_request('sort_dir', 'desc');
$tasksearch = get_request('tasksearch', '');

$_allstatus = array (
    QUEUE_QUEUED, 
    QUEUE_FINISHED,
    QUEUE_FAILED, 
    QUEUE_RUNNING,
    QUEUE_PAUSED,
    QUEUE_CANCELLED,
    QUEUE_CRASH, 
    QUEUE_REMOVED
);

$times = array(
    0 => $LN['all'],
    1 => $LN['since'] . ' 1 ' . $LN['day'],
    2 => $LN['since'] . ' 2 ' . $LN['days'],
    7 => $LN['since'] . ' 1 ' . $LN['week'],
    14 => $LN['since'] . ' 2 ' . $LN['weeks'],
    30 => $LN['since'] . ' 1 ' . $LN['month'],
    60 => $LN['since'] . ' 2 ' . $LN['months'],
    365 => $LN['since'] . ' 1 ' . $LN['year']
);


$currentstatus = $status = get_request('status', '');
$timeval = get_request('time', 0);
if (!in_array($status, $_allstatus))
    $status = $currentstatus = '';
if (!in_array($timeval, array_keys($times)))
    $timeval = 1;
$qstatus = '';
if ($status != '') {
    $db->escape($status, TRUE);
        $qstatus = " AND \"status\" = $status";
}

if (!in_array($sort, array('description', 'progress', 'ETA', 'status', 'comment', 'lastupdate', 'starttime')))
    fatal_error('invalid sortname');
if (!in_array($sort_dir, array('asc', 'desc')))
    fatal_error('invalid sortname');

$qtime = '';
if ($timeval > 0) {
    $time = time() - ($timeval * 24 * 60 * 60) ; 
    $qtime = " AND \"lastupdate\" >= '$time'"; 
}

$max_cnt = get_request('max_count', 0);
if ($max_cnt == 0 || !is_numeric($max_cnt))
    $max_cnt = $prefs['setsperpage'];

$sql  = "* FROM queueinfo WHERE 1=1 $qtime $qstatus ORDER BY \"$sort\" $sort_dir";
$res = $db->select_query($sql, 2 * $max_cnt);

$tasks = array();

if ($res !==  FALSE) {
    $cnt = 1;
	foreach ($res as $row) {
		$description = $row['description'];
		$progress = $row['progress'];
		$ETA = $row['ETA'];
		$task['progress'] = $progress;
		$task['urdd_id'] = $row['urdd_id'];
        $task['status'] = isset($LN['transfers_status_' . strtolower($row['status'])]) ?$LN['transfers_status_' . strtolower($row['status'])] : '?';
        $task['raw_status'] = $row['status'];
		$task['comment'] = $row['comment'];
		$task['added'] = time_format($row['starttime']);
        $task['lastupdated'] = time_format($row['lastupdate']);
        $description = command_description($db, $description);
        if ($tasksearch != '')
            if (!stristr($description, $tasksearch))
                continue;
		if (strlen($description) > $prefs['maxsetname']) {
			$task_short = substr($description, 0, max(0, $prefs['maxsetname']-3) ) . '...';
			$task_popup = $description;
		} else {
			$task_short = $description;
			$task_popup = '@';
		}

		if ($progress == '100' || $ETA == 0) 
			$ETA = '';
		else 
			$ETA = sec2min($ETA);
		$task['eta'] = $ETA;
		$task['description'] = $task_short;
		$task['task_popup'] = $task_popup;

        $tasks[] = $task;
        if ($cnt >= $max_cnt) break;
        $cnt++;
	}
}


$challenge = set_challenge();
$smarty->assign('challenge',		$challenge);
$smarty->assign('alltasks',	$tasks);
$smarty->assign('order',	$sort);
$smarty->assign('order_dir',	$sort_dir);
$smarty->assign('urdd_online',    	(int)$urdd_online);
$smarty->assign('max_count',    	$max_cnt);
$smarty->display('ajax_admintasks.tpl');

?>
